﻿using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace BiocityFilmquizAdmin.Src.DatabaseAccess
{
    /// <summary>
    /// Summary description for DbAcces
    /// </summary>
    public class DbAcces
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
        private static DbAcces instance = null;
        private string connectionString;

        private DbAcces()
        {
            initialize();
        }

        public static DbAcces getInstance()
        {
            if (instance == null)
            {
                instance = new DbAcces();
            }

            return instance;
        }

        private void initialize()
        {
            server = Statics.getDataBaseUrl();
            database = Statics.getDataBaseScheme();
            uid = Statics.getDataBaseUser();
            password = Statics.getDataBasePassword();
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        }

        private bool OpenConnection()
        {
            try
            {
                connection = new MySqlConnection(connectionString);
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                connection.Close();
                switch (ex.Number)
                {
                    case 0:
                        break;

                    case 1045:
                        break;
                }
                return false;
            }
        }

        public bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        public void Insert(String query)
        {
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                this.CloseConnection();
            }
        }

        public void Update(String query)
        {
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = query;
                cmd.Connection = connection;
                cmd.ExecuteNonQuery();
                this.CloseConnection();
            }
        }

        public void Delete(String query)
        {
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                this.CloseConnection();
            }
        }

        /// <summary> Standart select method for database access. 
        /// Always remember to call closeConnection() when finished with reader, to prevent connection locking.</summary>
        public MySqlDataReader Select(String query)
        {
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                return dataReader;
            }
            else
            {
                return null;
            }
        }

    }
}